package com.zola.blog.gateway.config1;

/**
 * @author zehua
 */

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import reactor.core.publisher.Mono;

/**
 * @author zehua
 * @Date: 2022/1/3 - 01 -03 - 21:23
 * @Description: 全局过滤器
 * @Version: 1.0
 */
@Slf4j
@Configuration
public class GlobalFilterConfig {

    /**
     * order 越小，越先执行
     */
    @Bean
    @Order(-1)
    public GlobalFilter globalFilter1() {
        return (exchange, chain) -> {
            log.info("pro filter globalFilter1...");
            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                log.info("post filter globalFilter1...");
            }));
        };
    }

    @Bean
    @Order(1)
    public GlobalFilter globalFilter2() {
        return (exchange, chain) -> {
            log.info("pro filter globalFilter2...");
            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                log.info("post filter globalFilter2...");
            }));
        };
    }
}
